import { getToken, removeToken, removeUsername } from "@/api/app.js";
import router from "@/router/index.js";
import { loginStore } from '@/store/login.js';

const whiteRouter = ['/login']

router.beforeEach((to, from, next) => {
    // token验证，如果getToken得到值，就跳转，如果没有就不跳转
    const store = loginStore()

    if (getToken()) {
        if (to.path === '/login') {
            removeToken()
            removeUsername()
            store.token = ''
            store.username = ''
            next()
        } else {
            next()
        }
    } else {
        if (whiteRouter.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
        }
    }
})